using System.Collections.Generic;
using Photon;
using UnityEngine;

public class Border : Photon.MonoBehaviour
{
	public List<GameObject> collidersInside = new List<GameObject>();

	private void Start()
	{
	}

	private void Update()
	{
	}

	private void OnTriggerStay(Collider collider)
	{
		if (collidersInside.Contains(collider.gameObject))
		{
			return;
		}
		Debug.Log("COllider  " + collider.gameObject.tag + " " + collider.gameObject.name);
		if (collider.gameObject.tag == "Player")
		{
			Debug.Log("COllider PLayer " + collider.gameObject.tag + " " + collider.gameObject.name);
			collidersInside.Add(collider.gameObject);
			if (PhotonNetwork.room != null && !PhotonNetwork.offlineMode)
			{
				if (collider.GetComponent<PlayerSetup>().photonView.isMine)
				{
					Manager.Instance.s_PlayerSetup.RespawnPlayer();
					collidersInside.Remove(collider.gameObject);
				}
			}
			else
			{
				Manager.Instance.s_PlayerSetup.RespawnPlayer();
			}
			collidersInside.Remove(collider.gameObject);
		}
		else
		{
			if (!(GetPerent(collider.transform).tag == "Car") && !(GetPerent(collider.transform).tag == "Item"))
			{
				return;
			}
			GameObject gameObject = GetPerent(collider.transform).gameObject;
			collidersInside.Add(gameObject);
			if (PhotonNetwork.room != null && !PhotonNetwork.offlineMode)
			{
				if (gameObject.GetComponent<ItemFunctions>().photonView.isMine)
				{
					PhotonNetwork.Destroy(gameObject);
				}
			}
			else
			{
				Object.Destroy(gameObject);
			}
		}
	}

	private Transform GetPerent(Transform tr)
	{
		if (tr.parent == null)
		{
			return tr;
		}
		return GetPerent(tr.parent);
	}
}
